package cn.edu.nju.ws.sview.wyge.servlets;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import cn.edu.nju.ws.sview.wyge.relations.adataset.overview.GetDotGraph;
import cn.edu.nju.ws.sview.wyge.relations.adataset.overview.PrintGraph;

public class DatasetOverview extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String json = null;
		response.setCharacterEncoding("utf-8");
		String out = "";
		if (request.getParameter("get") != null) {
			json = request.getParameter("get");
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			String ds_type = (String) regtJSON.get("ds_type");
			int ds_id = Integer.valueOf((String) regtJSON.get("ds_id"));
			String tmpDirectory = URLDecoder.decode(
					(String) regtJSON.get("tmpDirectory"), "utf-8");
			try {
				GetDotGraph.get(tmpDirectory, ds_type, ds_id);
				int[] dem = PrintGraph.print(tmpDirectory, ds_type, ds_id);
				int maxwidth = 850;
				int maxheight = 350;

				int width = dem[0];
				int height = dem[1];

				if ((double) width / height > (double) maxwidth / maxheight) {
					if (width > maxwidth) {
						width = maxwidth;
						height = maxheight;
					}
				} else {
					if (height > maxheight) {
						height = maxheight;
						width = maxwidth;
					}
				}

				out = "<embed src=\"pics/"
						+ ds_type
						+ ds_id
						+ ".svg\" height=\""
						+ height
						+ "\""
						+ " width=\""
						+ width
						+ "\""
						+ " type=\"image/svg+xml\""
						+ " pluginspage=\"http://www.adobe.com/svg/viewer/install/\" />";
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		response.getWriter().print(out);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
